package com.shuaixin.web.service.impl;

import com.shuaixin.web.dao.MessageDaoI;
import com.shuaixin.web.model.Message;
import com.shuaixin.web.service.MessageServiceI;
import com.shuaixin.web.util.Distance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by wby on 2015/5/23.
 */
@Service
public class MessageServiceImpl implements MessageServiceI {

    @Autowired
    private MessageDaoI messageDao;

    @Override
    public String addMessage(Message message) {
        Serializable uuid = messageDao.save(message);
        if (uuid == null) {
            return null;
        }
        return uuid.toString();
    }

    @Override
    public List<Message> getMessage(int page, int rows, double lont, double latt, double distance) {

        double lonth = lont + distance;
        double lontl = lont - distance;
        double latth = latt + distance;
        double lattl = latt - distance;
        String hql = "from Message t where t.lon > " + lontl + " and t.lon < " + lonth + "  and t.lat>" + lattl + "  and t.lat<" + latth + "";
        return messageDao.find(hql, page, rows);
    }

    @Override
    public long getMessageCount(double lont, double latt, double distance) {

        double lonth = lont + distance;
        double lontl = lont - distance;
        double latth = latt + distance;
        double lattl = latt - distance;
        String hql = "select count(*) from Message t where t.lon > " + lontl + " and t.lon < " + lonth + "  and t.lat>" + lattl + "  and t.lat<" + latth + "";
        return messageDao.count(hql);
    }

    @Override
    public List<Message> getgetNeabyInfoMessage(double lon, double lat, double distance) {
        List<Message>  listmsg = messageDao.find("from Message");
        List<Message>  back_listmsg = new LinkedList<Message>();
        for(Message msg : listmsg){
            double dis = Distance.GetDistance(msg.getLon(),msg.getLat(),lon,lat);

            if(dis <= distance){
                back_listmsg.add(msg);
                System.out.println(msg.toString());
            }
        }
        return back_listmsg;
    }


}
